package com.sky.controller.subAccount;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.constant.JwtClaimsConstant;
import com.sky.entity.futu.StockHolding;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.StockHoldingService;
import com.sky.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 我的持仓 前端控制器
 * </p>
 *
 * @author GHOSTER by RemSynch
 * @since 2024-11-06
 */
@RestController
@RequestMapping("/stockHolding")
public class StockHoldingMirrorController {

    @Autowired
    private StockHoldingService stockHoldingService;

    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping
    public Result list(HttpServletRequest request) {
        String token = request.getHeader(jwtProperties.getSubAccountsTokenName());
        Claims claims = JwtUtil.parseJWT(jwtProperties.getSubAccountsSecretKey(), token);
        String userId = claims.get(JwtClaimsConstant.ACCOUNT_ID).toString();
        LambdaQueryWrapper<StockHolding> stockHoldingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        return Result.success(stockHoldingService.list(stockHoldingLambdaQueryWrapper.eq(StockHolding::getUserId, userId)));
    }

}
